  <template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="名称" prop="bookName">
        <el-input
          v-model="queryParams.bookName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="封面" prop="imgUrl">-->
<!--        <el-input-->
<!--          v-model="queryParams.imgUrl"-->
<!--          placeholder="请输入封面"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="分类" prop="bookCategory">-->
<!--        <el-input-->
<!--          v-model="queryParams.bookCategory"-->
<!--          placeholder="请输入分类"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-form-item label="分类" prop="bookCategory">
          <el-select v-model="queryParams.bookCategory" placeholder="请选择类型:" clearable>
            <el-option
              v-for="dict in categorys"
              :key="dict.id"
              :label="dict.categoryName"
              :value="dict.id"
            />
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="标签" prop="tipLabel" style="margin-right: 10%;">
        <el-input
          v-model="queryParams.tipLabel"
          placeholder="请输入标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      
<!--      <el-form-item label="排序" prop="bookIndex">-->
<!--        <el-input-->
<!--          v-model="queryParams.bookIndex"-->
<!--          placeholder="请输入排序"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bookinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:bookinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bookinfo:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:bookinfo:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" sortable="" /> -->
      <el-table-column label="名称" align="center" prop="bookName" width="150"/>
<!--      <el-table-column label="封面" align="center" prop="imgUrl" />-->
      <el-table-column label="封面" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="封面地址" align="center" prop="imgUrl" width="300"/>
      <el-table-column label="缩略图地址" align="center" prop="abbreviate" width="300"/>
      <el-table-column label="出版社" align="center" prop="publisher" />
      <el-table-column label="分类" align="center" prop="bookCategory" :formatter="categoryFomatter"/>
      <!-- <el-table-column label="标签" align="center" prop="tipLabel" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="排序" align="center" prop="id" /> -->
      <el-table-column label="排序" align="center" prop="bookIndex" sortable="">
        <template slot-scope="scope">
          {{ scope.row.bookIndex?scope.row.bookIndex:"0" }}
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="status" :formatter="statusFomatter">
      <template slot-scope="scope">
        <span>{{ scope.row.status == 1 ? '有效' : '无效' }}</span>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bookinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bookinfo:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleAddMusic(scope.row)"
          >乐曲信息</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改书籍信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="封面" prop="imgUrl">
          <el-upload class="upload-demo" :action="upUrl" name="multipartFile" 
          :on-change="onChangeFile" v-model="form.imgUrl" ref="uploadComp"
          :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" :limit="2"
          :on-exceed="handleExceed" :on-success="hanldeSuccess" :headers="headers" :show-file-list="true"
          :auto-upload="true">
          <el-button type="warning" plain icon="el-icon-upload" size="mini" @click="handleImportMusic">封面</el-button>
        </el-upload>
        </el-form-item>
        <el-form-item label="书籍封面" prop="abbreviate">
          <el-upload class="upload-demo" :fileType="fileType" :fileSize="fileSize" :action="upUrl" v-model="form.abbreviate"
            name="multipartFile" ref="uploadComp5" :on-change="onChangeFile" :on-preview="handlePreview"
            :on-remove="handleRemove" :before-remove="beforeRemove" :limit="2" :on-exceed="handleExceed"
            :on-success="hanldeSuccess5" :headers="headers" :show-file-list="true" :auto-upload="true">
            <el-button type="warning" plain icon="el-icon-upload" size="mini" @click="handleImportMusic">封面缩略图</el-button>
          </el-upload>
        </el-form-item>


        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社" />
        </el-form-item>

        <el-form-item label="分类" prop="bookCategory">
          <el-select v-model="form.bookCategory" placeholder="请选择类型:" clearable>
            <el-option
              v-for="dict in categorys"
              :key="dict.id"
              :label="dict.categoryName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="标签" prop="tipLabel">
          <el-input v-model="form.tipLabel" placeholder="请输入标签" />
        </el-form-item>


        <el-form-item label="顺序" prop="bookIndex">
          <el-input v-model="form.bookIndex" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="form.description" placeholder="请输入简介" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBookinfo, getBookinfo, delBookinfo, addBookinfo, updateBookinfo } from "@/api/system/bookinfo";
import { listBookcategory } from "@/api/system/bookcategory";
export default {
  name: "Bookinfo",
  data() {
    return {
      
      upUrl: process.env.VUE_APP_BASE_API + '/v1/app/public/upload',
      categorys:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 书籍信息表格数据
      bookinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null,
        imgUrl: null,
        publisher: null,
        bookCategory: null,
        tipLabel: null,
        bookIndex: null,
        status: null
      },
       queryParam: {
              pageNum: 1,
              pageSize: 30,
            },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getBookCategory()
    this.getList();
  },
  methods: {

    handleSuccess(response, file, fileList) {
    // 处理成功上传
    if (response.code === 200) {
      // 假设您的API响应包含已上传文件的URL
      this.form.imgUrl = response.data.url;
      this.$message.success('上传成功');
    } else {
      this.$message.error('上传失败');
    }
  },


    getBookCategory(){
      listBookcategory(this.queryParam).then(response =>{
        this.categorys = response.rows;
      })
    },
    /** 查询书籍信息列表 */
    getList() {
      this.loading = true;
      listBookinfo(this.queryParams).then(response => {
        this.bookinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.uploadComp.clearFiles()
      this.$refs.uploadComp5.clearFiles()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bookName: null,
        imgUrl: null,
        publisher: null,
        bookCategory: null,
        tipLabel: null,
        bookIndex: null,
        status: null,
        abbreviate:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加书籍信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBookinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改书籍信息";
      });
    },


    handleImportMusic() {
      
      console.log("~~@@@@@@~~~~~")
    },
    handleRemove(multipartFile, multipartFileList) {
      console.log(multipartFile, multipartFileList);
    },
    handlePreview(multipartFile) {
      console.log(multipartFile);
    },
    handleExceed(multipartFiles, multipartFileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${multipartFiles.length} 个文件，共选择了 ${multipartFiles.length + multipartFileList.length} 个文件`);
    },
    beforeRemove(multipartFile, multipartFileList) {
      return this.$confirm(`确定移除 ${multipartFile.name}？`);
    },
    hanldeSuccess(res,multipartFile, multipartFileList) {
      if(res.code == 200){
        this.form.imgUrl = res.data.url
      }
      // this.getList()
    },
    hanldeSuccess5(res, multipartFile, multipartFileList) {
      if (res.code == 200) {
        this.form.abbreviate = res.data.url
      }
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBookinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.$refs.uploadComp.clearFiles()
            addBookinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.$refs.uploadComp.clearFiles()
              this.$refs.uploadComp5.clearFiles()
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除书籍信息编号为"' + ids + '"的数据项？').then(function() {
        return delBookinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bookinfo/export', {
        ...this.queryParams
      }, `bookinfo_${new Date().getTime()}.xlsx`)
    },
      handleAddMusic(row){
          const bookId = row.id;
          this.$router.push("/system/bookMusic/list/" + bookId);
      },
      categoryFomatter(row,col){
         for(let i =0;i<this.categorys.length;i++){
             let item = this.categorys[i]
             if(row.bookCategory == item.id){
                 return item.categoryName
             }
         }
      },
      statusFomatter(row){
        if(row.status == 1)
        {
            return '在线'
        }
          else{
              return ''
        }
      }
  }
};
</script>
<!-- <style>
.form{
  display: flex;
  flex-direction: column;
}
.form>el-form-item{
  width: 150px;
}
</style> -->